package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class RegDemo {
    public static void main(String[] args) throws Exception {

      /*  Scanner scan=new Scanner(System.in);
        System.out.println("请输入注册姓名：");
        String username= scan.nextLine();
        System.out.println("请输入注册密码：");
        String passWord=scan.nextLine();
        System.out.println("请输入注册昵称：");
        String nickName=scan.nextLine();
        System.out.println("请输入年龄：");
        int age= scan.nextInt();
       User user=new User();
       user.setUserName(username);
       user.setNickName(nickName);
       user.setPassWord(passWord);
       user.setAge(age);
       insert(user);*/
        User user=InputUtil.getInputObject(new User(),"欢迎注册");
        System.out.println(user);
        insert(user);

    }
    public static void insert(User user) throws Exception{
        System.out.println("正在连接数据库...");
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root");
        System.out.println("数据库连接成功！");
        Statement statement = connection.createStatement();
        String sql = "INSERT INTO user( username, password, nickname, age) VALUES ('" +
                user.getUserName()+"','" +
                user.getPassWord()+ "','" +
                user.getNickName()+"'," +
                user.getAge()+")";

        System.out.println(sql);
        int num = statement.executeUpdate(sql);
        System.out.println(num > 0 ? "存取数据成功" : "存取数据失败");
        connection.close();
    }
}
